package org.jeecg.modules.tls.oes.exampaperanswer.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.modules.tls.oes.exam.service.IOesExamService;
import org.jeecg.modules.tls.oes.exampaperanswer.entity.OesExamPaperAnswer;
import org.jeecg.modules.tls.oes.exampaperanswer.mapper.OesExamPaperAnswerMapper;
import org.jeecg.modules.tls.oes.exampaperanswer.service.IOesExamPaperAnswerService;
import org.jeecg.modules.tls.oes.exampaperquestioncustomeranswer.entity.OesExamPaperQuestionCustomerAnswer;
import org.jeecg.modules.tls.oes.exampaperquestioncustomeranswer.mapper.OesExamPaperQuestionCustomerAnswerMapper;
import org.jeecg.modules.tls.oes.question.entity.OesQuestion;
import org.jeecg.modules.tls.oes.question.mapper.OesQuestionMapper;
import org.jeecg.modules.tls.oes.textcontent.entity.OesTextContent;
import org.jeecg.modules.tls.oes.textcontent.mapper.OesTextContentMapper;
import org.jeecg.modules.tls.oes.utils.JsonUtil;
import org.jeecg.modules.tls.oes.viewmodel.admin.exam.ExamRequestVM;
import org.jeecg.modules.tls.oes.viewmodel.student.answer.AnswerPaperVM;
import org.jeecg.modules.tls.oes.viewmodel.student.answer.AnswerVM;
import org.jeecg.modules.tls.oes.viewmodel.student.answer.ExamPaperSubmitVM;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: oes_exam_paper_answer
 * @Author: jeecg-boot
 * @Date:   2022-07-19
 * @Version: V1.0
 */
@Service
public class OesExamPaperAnswerServiceImpl extends ServiceImpl<OesExamPaperAnswerMapper, OesExamPaperAnswer> implements IOesExamPaperAnswerService {
    @Autowired
    OesExamPaperAnswerMapper oesExamPaperAnswerMapper;
    @Autowired
    IOesExamService iOesExamService;
    @Autowired
    OesExamPaperQuestionCustomerAnswerMapper oesExamPaperQuestionCustomerAnswerMapper;
    @Autowired
    OesQuestionMapper oesQuestionMapper;
    @Autowired
    OesTextContentMapper oesTextContentMapper;
    //题目数量
    Integer questionCount;
    //做对数量
    Integer questionCorrect;
    //系统判分
    Integer systemScore;
    @Override
    public Integer sumbitExamPaperAnswer(ExamPaperSubmitVM model) {
        this.questionCount=0;
        this.questionCorrect=0;
        this.systemScore=0;
        //插入答卷信息
        ExamRequestVM examPaper = iOesExamService.getExamRequestVM(model.getExamId());
        OesExamPaperAnswer examPaperAnswer = new OesExamPaperAnswer();
        examPaperAnswer.setExamId(examPaper.getId());
        examPaperAnswer.setPaperId(examPaper.getPaperId());
        examPaperAnswer.setPaperName(examPaper.getPaper().getName());
        examPaperAnswer.setExamName(examPaper.getName());
        //examPaperAnswer.setPaperType(examPaper.getPaperType());
        //examPaperAnswer.setSubjectId(examPaper.getSubjectId());
        examPaperAnswer.setPaperScore(examPaper.getPaper().getScore());
        examPaperAnswer.setDoTime(model.getDoTime());
        examPaperAnswer.setStatus(1);
        examPaperAnswer.setCreateTime(DateUtil.date());
        examPaperAnswer.setCreateUser(model.getUserId());
        oesExamPaperAnswerMapper.insert(examPaperAnswer);

        //插入答卷题目信息
        List<OesExamPaperQuestionCustomerAnswer> examPaperQuestionCustomerAnswerList = model.getAnswerItems().stream().map(i->{
            this.questionCount += 1;
            OesExamPaperQuestionCustomerAnswer examPaperQuestionCustomerAnswer = new OesExamPaperQuestionCustomerAnswer();
            OesQuestion question = oesQuestionMapper.selectById(i.getQuestionId());
            examPaperQuestionCustomerAnswer.setQuestionId(question.getId());
            //examPaperQuestionCustomerAnswer.setExamPaperId(examPaper.getId());
            examPaperQuestionCustomerAnswer.setExamId(examPaper.getId());
            examPaperQuestionCustomerAnswer.setPaperId(examPaper.getPaperId());
            examPaperQuestionCustomerAnswer.setExamPaperAnswerId(examPaperAnswer.getId());
            examPaperQuestionCustomerAnswer.setQuestionType(question.getQuestionType());
            examPaperQuestionCustomerAnswer.setSubjectId(question.getSubjectId());
            examPaperQuestionCustomerAnswer.setQuestionScore(question.getScore());
            examPaperQuestionCustomerAnswer.setQuestionTextContentId(question.getInfoTextContentId());
            examPaperQuestionCustomerAnswer.setAnswer(i.getContent());
            if((i.getContentArray()!=null)&&i.getContentArray().size()>0){
                OesTextContent textContent = new OesTextContent();
                textContent.setContent(JsonUtil.toJsonStr(i.getContentArray()));
                textContent.setCreateTime(DateUtil.date());
                oesTextContentMapper.insert(textContent);
                examPaperQuestionCustomerAnswer.setTextContentId(textContent.getId());
            }
            if(question.getCorrect().equals(i.getContent())&&!i.getContent().equals("")){
                examPaperQuestionCustomerAnswer.setDoRight("1");
                examPaperQuestionCustomerAnswer.setCustomerScore(question.getScore());
                this.systemScore += question.getScore();
                this.questionCorrect +=1;
            }else {
                if((question.getQuestionType()==1)||(question.getQuestionType()==2)||(question.getQuestionType()==3)){
                    examPaperQuestionCustomerAnswer.setDoRight("0");
                }
                examPaperQuestionCustomerAnswer.setCustomerScore(0);
            }
            examPaperQuestionCustomerAnswer.setCreateTime(DateUtil.date());
            examPaperQuestionCustomerAnswer.setItemOrder(i.getItemOrder());
            examPaperQuestionCustomerAnswer.setCreateUser(model.getUserId());
            oesExamPaperQuestionCustomerAnswerMapper.insert(examPaperQuestionCustomerAnswer);
            return examPaperQuestionCustomerAnswer;
        }).collect(Collectors.toList());
        //更新答卷信息
        examPaperAnswer.setSystemScore(this.systemScore);
        examPaperAnswer.setUserScore(this.systemScore);
        examPaperAnswer.setQuestionCorrect(this.questionCorrect);
        examPaperAnswer.setQuestionCount(this.questionCount);
        oesExamPaperAnswerMapper.updateById(examPaperAnswer);
        return systemScore;
    }

    @Override
    public AnswerPaperVM getAnswerPaper(String id) {
        OesExamPaperAnswer examPaperAnswer = oesExamPaperAnswerMapper.selectById(id);
        ExamRequestVM examRequestVM = iOesExamService.getExamRequestVM(examPaperAnswer.getExamId());
        AnswerPaperVM answerPaperVM = new AnswerPaperVM();

        answerPaperVM.setExam(examRequestVM);

        AnswerVM answerVM = new AnswerVM();
        answerVM.setId(examPaperAnswer.getId());
        answerVM.setDoTime(examPaperAnswer.getDoTime());
        answerVM.setScore(examPaperAnswer.getUserScore());
        QueryWrapper<OesExamPaperQuestionCustomerAnswer> examPaperQuestionCustomerAnswerQueryWrapper = new QueryWrapper<>();
        examPaperQuestionCustomerAnswerQueryWrapper.eq("exam_paper_answer_id",examPaperAnswer.getId()).orderByAsc("item_order");
        List<OesExamPaperQuestionCustomerAnswer> answerItems = oesExamPaperQuestionCustomerAnswerMapper.selectList(examPaperQuestionCustomerAnswerQueryWrapper);
        for(OesExamPaperQuestionCustomerAnswer examPaperQuestionCustomerAnswer : answerItems){
            if(examPaperQuestionCustomerAnswer.getTextContentId()!=null){
                OesTextContent textContent = oesTextContentMapper.selectById(examPaperQuestionCustomerAnswer.getTextContentId());
                JSONArray array = JSONUtil.parseArray(textContent.getContent());
                examPaperQuestionCustomerAnswer.setContentArray(array.toList(String.class));
            }

        }
        answerVM.setAnswerItems(answerItems);
        answerPaperVM.setAnswer(answerVM);
        return answerPaperVM;
    }

    @Override
    public Integer updateExamAnswerPaper(ExamPaperSubmitVM examPaperSubmitVM) {
//        OesExamPaperAnswer examPaperAnswer = oesExamPaperAnswerMapper.selectById(examPaperSubmitVM.getId());
//
//        List<ExamPaperSubmitItemVM> judgeItems = examPaperSubmitVM.getAnswerItems().stream().filter(d -> d.getDoRight() == null).collect(Collectors.toList());
//
//        Integer customerScore = examPaperAnswer.getUserScore();
//        Integer questionCorrect = examPaperAnswer.getQuestionCorrect();
//
//        List<ExamPaperQuestionCustomerAnswer> examPaperAnswerUpdates = new ArrayList<>();
//        for (ExamPaperSubmitItemVM d : judgeItems) {
//            ExamPaperQuestionCustomerAnswer examPaperAnswerUpdate = new ExamPaperQuestionCustomerAnswer();
//            examPaperAnswerUpdate.setId(d.getId());
//            examPaperAnswerUpdate.setCustomerScore(d.getScore());
//            boolean doRight = examPaperAnswerUpdate.getCustomerScore().equals(d.getQuestionScore());
//            examPaperAnswerUpdate.setDoRight(doRight);
//            examPaperAnswerUpdates.add(examPaperAnswerUpdate);
//            customerScore += examPaperAnswerUpdate.getCustomerScore();
//            if (examPaperAnswerUpdate.getDoRight()) {
//                ++questionCorrect;
//            }
//            examPaperQuestionCustomerAnswerDao.updateById(examPaperAnswerUpdate);
//        }
//        examPaperAnswer.setUserScore(customerScore);
//        examPaperAnswer.setQuestionCorrect(questionCorrect);
//        examPaperAnswer.setStatus(2);
//        examPaperAnswerDao.updateById(examPaperAnswer);


        return null;
    }
}
